#include "Logger.h"

Logger::Logger (const char* zLogSubdir, const char* zLogPrefix, int iFlags)
{
   std::string zLogDir;
   _zLogPrefix = zLogPrefix;
   _iFlags = iFlags;
   _zLogBase = "";
   _zLogBase += "/log";

   mkdir((const char *)_zLogBase.c_str(),0775);


   _zLogDir = _zLogBase;
   if (zLogSubdir != NULL && zLogSubdir[0] != 0 && strcmp(zLogSubdir,".") != 0)
   {
      _zLogDir += "/";
      _zLogDir += zLogSubdir;
      mkdir((const char *)_zLogDir.c_str(),0775);
   }

   _pLogFile = NULL;//EnsureProperLogFileOpen();
}

Logger::~Logger()
{
   if (_pLogFile->is_open() )
   {

   }
}

int Logger::LogPrintf (const char* zFmt, ... )
{
   int iRC;
   va_list args;
   va_start(args, zFmt);
   iRC = vLogPrintf(zFmt,args);
   va_end(args);
   return iRC;
}


int Logger::vLogPrintf (const char *zFmt, va_list args)
{
   int iRC = 0;

   char zDateTime[32];  // big enough for "MM/DD/YYYY hh:mm:ss"


   EnsureProperLogFileOpen();
   if ( _pLogFile )
   {
	  char temp[256] = {'\0'};
      GetNewDateTime(NULL, NULL, zDateTime);
      sprintf(temp,"%-5d %s ", pthread_self(), zDateTime);
      vsnprintf(temp,sizeof(temp),zFmt,args);
      //_pLogFile->flush();
   }

   return iRC;
}

int Logger::GetNewDateTime (struct tm* pLastTime, char* szDate, char *szDateTime)
{
   time_t iNow;
   struct tm *pThisTime;
   int iNewDate;

   iNow = time(NULL);
   pThisTime = localtime(&iNow);
   iNewDate = 0;

   if (pLastTime != NULL)
   {
	   if (pThisTime->tm_mday != pLastTime->tm_mday ||
          pThisTime->tm_mon  != pLastTime->tm_mon  ||
          pThisTime->tm_year != pLastTime->tm_year)
         iNewDate = 1;
      memcpy(pLastTime,pThisTime,sizeof(struct tm));
   }
   if (szDate != NULL)


   {
      sprintf(szDate,"%04d%02d%02d",pThisTime->tm_year+1900,pThisTime->tm_mon+1,pThisTime->tm_mday);
   }
   if (szDateTime != NULL)
   {
      sprintf(szDateTime,"%02d/%02d/%04d %02d:%02d:%02d ",
         pThisTime->tm_mon+1,
         pThisTime->tm_mday,
         pThisTime->tm_year+1900,
         pThisTime->tm_hour,
         pThisTime->tm_min,
         pThisTime->tm_sec);
   }
   return(iNewDate);
}

bool Logger::EnsureProperLogFileOpen ()
{
   char zDateStamp[12];  // big enough for YYYYMMDD
   int iNewDate;

   if ( !_pLogFile->is_open() )
   {
      iNewDate = GetNewDateTime(&_sLastTime,zDateStamp,NULL);
      if ( !(bool)(_pLogFile->good()) && !iNewDate)
         return false;
      _zLogFileName = _zLogDir;
      _zLogFileName += "/";
      _zLogFileName += _zLogPrefix;
      _zLogFileName += zDateStamp;
      _zLogFileName += ".log";
      _pLogFile->open(platform::native_path(_zLogFileName.data()).c_str());


      if ( !_pLogFile->is_open() || !_pLogFile->good() )
    	  return false;
   }
	   return true;
}

std::string platform::native_path(std::string str)
{
	return str;
}

void Logger::nothingtodo()
{
	std::cout << std::endl << "NO EXCEPTIONS" << std::endl;
}

void Logger::Change(const char* str)
{
	_zLogFileName = str;
	_pLogFile->open(platform::native_path(_zLogFileName.data()).c_str());
}
